package com.jiawa.train.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.util.SnowUtil;
import com.jiawa.train.common.util.JwtUtil;
import com.jiawa.train.domain.Member;
import com.jiawa.train.domain.MemberExample;
import com.jiawa.train.mapper.MemberMapper;
import com.jiawa.train.req.MemberLoginReq;
import com.jiawa.train.req.MemberRegisterReq;
import com.jiawa.train.req.MemberSendCodeReq;
import com.jiawa.train.resp.MemberLoginResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberService {
	@Autowired
	private MemberMapper memberMapper;
	private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);
	public Long count(){
		return memberMapper.countByExample(null);
	}
	public long register(MemberRegisterReq req){
		Member memberDB = selectByMobile(req.getMobile());

		if (ObjectUtil.isNotNull(memberDB)) {
			// return list.get(0).getId();
			throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
		}

		Member member = new Member();
		member.setId(SnowUtil.getSnowflakeNextId());
		member.setMobile(req.getMobile());
		memberMapper.insert(member);
		return member.getId();
	}

	public void sendCode(MemberSendCodeReq req){
		Member memberDB = selectByMobile(req.getMobile());

		if (ObjectUtil.isNull(memberDB)) {
			LOG.info("手机号不存在,插入一条记录");
			Member member = new Member();
			member.setId(SnowUtil.getSnowflakeNextId());
			member.setMobile(req.getMobile());
			memberMapper.insert(member);
		}else{
			LOG.info("手机号存在，不操作");
		}
		//生成验证码
		String code = RandomUtil.randomString(4);
		LOG.info("生成短信验证码:{}", code);
		//保存短信记录
		LOG.info("保存短信记录");
		//对接短信通道，发送短信
		LOG.info("对接短信通道");
	}
	public MemberLoginResp login(MemberLoginReq req){
		String mobile = req.getMobile();
		String code = req.getCode();

		Member memberDB = selectByMobile(mobile);

		if (ObjectUtil.isNull(memberDB)) {
			throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
		}
		//校验短信验证码
		if(!"8888".equals(code)){
			throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
		}
		MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);

		String token = JwtUtil.createToken(memberLoginResp.getId(),memberLoginResp.getMobile());
		memberLoginResp.setToken(token);
		return memberLoginResp;
	}

	private Member selectByMobile(String mobile) {
		MemberExample memberExample = new MemberExample();
		memberExample.createCriteria().andMobileEqualTo(mobile);
		List<Member> list = memberMapper.selectByExample(memberExample);
		if (CollUtil.isEmpty(list)) {
			return null;
		}else {
			return list.get(0);
		}
	}
}
